﻿using System;
using System.Collections;
using System.Data;
using Lib.Data;
using Lib.Utils;


namespace SysObj
{
    public class Bh_BanHang : Lib.Bases.BaseClass
    {
        Bh_BanHang_HangHoa _ctiet = new Bh_BanHang_HangHoa();

        #region Prop
        public System.Int32 IdBanHang
        {
            get;
            set;
        }

        public System.DateTime Ngay
        {
            get;
            set;
        }

        public System.Int32 IdBan
        {
            get;
            set;
        }

        public System.Decimal ThanhTien
        {
            get;
            set;
        }

        public System.Int32 PT_CK
        {
            get;
            set;
        }

        public System.Decimal Tien_CK
        {
            get;
            set;
        }

        public System.Decimal TongTien
        {
            get;
            set;
        }

        public System.Decimal KhachTra
        {
            get;
            set;
        }

        public System.String GhiChu
        {
            get;
            set;
        }

        #endregion

        //Called from Bh_BanHang_GopBan
        public bool NoneTransaction { get; set; }

        public DataTable D_HH { get; set; }

        #region  Constructors
        public Bh_BanHang()
        {
            m_DataLayer = new Lib.Data.DataLayer();
        }

        #endregion

        #region Methods
        public override void ResetData()
        {
            IdBanHang = 0;
            Ngay = DateTime.Now;
            IdBan = 0;
            ThanhTien = 0;
            PT_CK = 0;
            Tien_CK = 0;
            TongTien = 0;
            KhachTra = 0;
            GhiChu = "";
        }

        public override DataTable LoadData()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = m_DataLayer.ExecDataTable("SP_Bh_BanHang", " Command,IdBanHang", "LoadData", IdBanHang);
                this.ResetData();
                if (dt == null || dt.Rows.Count <= 0)
                {
                    return null;
                }
                this.FillData(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable LoadDataByTableID()
        {
            DataTable dt = m_DataLayer.ExecDataTable("SP_Bh_BanHang", " Command,IdBan", "LoadDataByTableID", IdBan);
            ResetData();
            if (dt != null && dt.Rows.Count > 0)
            {
                this.FillData(dt.Rows[0]);
            }
            return _ctiet.GetDetail(m_DataLayer, IdBanHang);
        }

        public DataTable GetTableHistory(int v_IdBan, DateTime v_Ngay)
        {
            return m_DataLayer.ExecDataTable("SP_Bh_BanHang", " Command,IdBan,Ngay", "GetTableHistory", v_IdBan, v_Ngay);
        }

        public DataTable GetDetail()
        {
            return _ctiet.GetDetail(m_DataLayer, IdBanHang);
        }

        public DataTable GetHistory(DateTime v_Date)
        {
            return m_DataLayer.ExecDataTable("SP_Bh_BanHang", " Command,Ngay", "GetHistory", v_Date);
        }

        public DataTable GetDataPrint(int v_IdBanHang)
        {
            return m_DataLayer.ExecDataTable("SP_Bh_BanHang", " Command,IdBanHang", "GetDataPrint", v_IdBanHang);
        }

        public DataTable GetSummary(int v_IdBanHang)
        {
            DataTable dt = m_DataLayer.ExecDataTable("SP_Bh_BanHang", " Command,IdBanHang", "GetSummary", v_IdBanHang);
            ResetData();
            if (dt != null && dt.Rows.Count > 0)
            {
                this.FillData(dt.Rows[0]);
            }

            return _ctiet.GetSummary(m_DataLayer, IdBanHang);
        }

        public override void FillData(DataRow row)
        {
            ResetData();
            if (row.Table.Columns.Contains("IdBanHang"))
                IdBanHang = Lib.Data.ValueType.GetValue(row["IdBanHang"], IdBanHang);
            if (row.Table.Columns.Contains("Ngay"))
                Ngay = Lib.Data.ValueType.GetValue(row["Ngay"], Ngay);
            if (row.Table.Columns.Contains("IdBan"))
                IdBan = Lib.Data.ValueType.GetValue(row["IdBan"], IdBan);
            if (row.Table.Columns.Contains("ThanhTien"))
                ThanhTien = Lib.Data.ValueType.GetValue(row["ThanhTien"], ThanhTien);
            if (row.Table.Columns.Contains("PT_CK"))
                PT_CK = Lib.Data.ValueType.GetValue(row["PT_CK"], PT_CK);
            if (row.Table.Columns.Contains("Tien_CK"))
                Tien_CK = Lib.Data.ValueType.GetValue(row["Tien_CK"], Tien_CK);
            if (row.Table.Columns.Contains("TongTien"))
                TongTien = Lib.Data.ValueType.GetValue(row["TongTien"], TongTien);
            if (row.Table.Columns.Contains("KhachTra"))
                KhachTra = Lib.Data.ValueType.GetValue(row["KhachTra"], KhachTra);
            if (row.Table.Columns.Contains("GhiChu"))
                GhiChu = Lib.Data.ValueType.GetValue(row["GhiChu"], GhiChu);
        }

        public void UpdateNewTable(int v_IdBanHang, int v_IdBanMoi)
        {
            m_DataLayer.ExecNonQuery("Sp_Bh_BanHang", "Command,IdBanHang,IdBanMoi", "UpdateNewTable", v_IdBanHang, v_IdBanMoi);
        }

        public void UpdateSummary(int v_IdBanHang)
        {
            m_DataLayer.ExecNonQuery("Sp_Bh_BanHang", "Command,IdBanHang", "UpdateSummary", v_IdBanHang);
        }

        public override void OnUpdate()
        {
            if (!NoneTransaction)
                if (Lib.Lic.Trial.IsOver("Bh_BanHang", true)) return;

            Hashtable output = null;
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                if (!NoneTransaction)
                    m_DataLayer.BeginTransaction();

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Update");
                Lib.Data.ParamHolder.AddParam("IdBanHang", IdBanHang);
                Lib.Data.ParamHolder.AddParam("Ngay", Ngay);
                Lib.Data.ParamHolder.AddParam("IdBan", IdBan);
                Lib.Data.ParamHolder.AddParam("ThanhTien", ThanhTien);
                Lib.Data.ParamHolder.AddParam("PT_CK", PT_CK);
                Lib.Data.ParamHolder.AddParam("Tien_CK", Tien_CK);
                Lib.Data.ParamHolder.AddParam("TongTien", TongTien);
                Lib.Data.ParamHolder.AddParam("KhachTra", KhachTra);
                Lib.Data.ParamHolder.AddParam("GhiChu", GhiChu);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_BH_BANHANG", s_ParamName, ref output, o_ParamValue);

                if (!object.Equals(null, output))
                    IdBanHang = Cvert.ToInt32(output["IdBanHang"], 0);

                _ctiet.OnDelete(m_DataLayer, IdBanHang);
                _ctiet.Insert(m_DataLayer, D_HH, IdBanHang);

                if (!NoneTransaction)
                    m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                if (!NoneTransaction)
                    m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }

        public void OnRemoveMenu(int v_IdHangHoa)
        {
            _ctiet.OnDeleteMenu(m_DataLayer, this.IdBanHang, v_IdHangHoa);
        }

        public override void OnDelete()
        {
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                _ctiet.OnDelete(m_DataLayer, IdBanHang);

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Delete");
                Lib.Data.ParamHolder.AddParam("IdBanHang", IdBanHang);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_BH_BANHANG", s_ParamName, o_ParamValue);

                m_DataLayer.CommitTransaction();

                ResetData();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }


        #endregion
    }
}
